package com.oldroid.apps.getit.ui;

import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.oldroid.apps.getit.R;
import com.oldroid.apps.getit.provider.DownloadsContract.Downloads;
import com.oldroid.apps.getit.service.DownloadWatcher;
import com.oldroid.apps.getit.util.AnalyticsUtils;
import com.oldroid.apps.getit.util.DropBox;


/**
 * HomeActivity
 * @author Taehyun Park
 *
 */
public class HomeActivity extends BaseActivity{
	private static final String TAG = "HomeActivity";
	
	static TextView details;
	
	static Context mContext;
	
	static int totalCount;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		details = (TextView) findViewById(R.id.the_status_detail);
		
		mContext = getApplicationContext();
		
		AnalyticsUtils.getInstance(this).trackPageView("/Home");
		setContentView(R.layout.activity_home);
		getActivityHelper().setupActionBar(null, 0);
		
		Intent watcherStart = new Intent(this, DownloadWatcher.class);
		startService(watcherStart);
		
		
		
		
		DropBox d = new DropBox();
		//registerBroadcast(); 
		
		
	}

	

	private void registerBroadcast(){
		final DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
		BroadcastReceiver br = new BroadcastReceiver() {
			
			@Override
			public void onReceive(Context context, Intent intent) {
//				 TODO Auto-generated method stub
				
				log("OnReceive: " + intent.getAction());//
				say("OnReceive: " + intent.getAction()); 
				
				if(true)
					return;//
				

//				if(intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)){
//					log("A Download has been completed"); 
//					say("A Download has been completed");
//					
//					
//					long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,0);
//					Query query = new Query();
//					query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL);
//					Cursor c = dm.query(query);
//					
//					int count = c.getCount();
//				
//					say("We have "+count + "completed downloads");
//					log("We have "+count + "completed downloads");
//                            
//					
//				}
				
			}
		};
		
		registerReceiver(br, new IntentFilter("android.intent.action.DOWNLOAD_COMPLETED"));
		
		Request request = new Request(
                Uri.parse("http://www.vogella.de/img/lars/LarsVogelArticle7.png"));
        dm.enqueue(request);
	}
	
	
	private void say(String msg){
		Toast.makeText(this, msg, Toast.LENGTH_SHORT);
	}
	
	private void log(String msg){
		Log.e("HOME", msg);
	}
	
	
	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onPostCreate(savedInstanceState);
		getActivityHelper().setupHomeActivity();
		
		if(true) // debuggin
			;//return;
		
		ContentResolver mURI = getContentResolver();
		String selection = null;
		String [] selectionArgs = null;
		String sortOrder = "ASC"; 
		sortOrder = null;
		
		Cursor c = mURI.query(Downloads.CONTENT_URI, null, selection, selectionArgs, sortOrder);
		startManagingCursor(c);
		int count = 0;//
			
		while(c.moveToNext()){
			
			
			String msg = count + ") " + c.getString(c.getColumnIndex(Downloads.FILENAME));
			say(msg);
			//log(msg);
			count ++;
			
			String name = c.getString(Downloads.COLUMN_INDEX_FILENAME);
			String fp = c.getString(Downloads.COLUMN_INDEX_URI);
			
			//log("Name = " + name + " | path = " + fp);
			

			
			
			
		}
		

		
		String text = "There are " + count + " records in the databse";
		totalCount = count;
		Toast.makeText(this, text, Toast.LENGTH_LONG);
		log(text);
		
		
		
		
		
		
	}
	
	
	private void deleteID(int id){
		
		getContentResolver().delete(Downloads.buildDownloadsUri(id), null, null);
	
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.refresh_menu_items, menu);
		super.onCreateOptionsMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == R.id.menu_refresh){
			// need to implement refresh probably not
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
	

}
